import request from 'utils/uni-pro.js';

function getNowFormatDate() {
	let myDate = new Date();
	let y, m, d;
	y = myDate.getFullYear(); // 年
	m = myDate.getMonth() + 1; // 月
	d = myDate.getDate(); // 日
	if (m < 10) {
		m = '0' + m;
	}
	if (d < 10) {
		d = '0' + d;
	}
	let hou = myDate.getHours();
	let min = myDate.getMinutes();
	if (hou < 10) {
		hou = '0' + hou;
	}
	if (min < 10) {
		min = '0' + min;
	}
	return y + "-" + m + "-" + d + " " + hou + ":" + min
}
//返回日期
function getFormatDate(data) {
	let myDate = new Date(data);
	let y, m, d;
	y = myDate.getFullYear(); // 年
	m = myDate.getMonth() + 1; // 月
	d = myDate.getDate(); // 日
	if (m < 10) {
		m = '0' + m;
	}
	if (d < 10) {
		d = '0' + d;
	}
	return y + "." + m + "." + d;
}

function getFormatDate1(data) {
	let myDate = new Date(data);
	let y, m, d;
	y = myDate.getFullYear(); // 年
	m = myDate.getMonth() + 1; // 月
	d = myDate.getDate(); // 日
	if (m < 10) {
		m = '0' + m;
	}
	if (d < 10) {
		d = '0' + d;
	}
	return y + "-" + m + "-" + d;
}
//返回时间
function getFormatDate2(data) {
	let myDate = new Date(data);
	let hou = myDate.getHours();
	let min = myDate.getMinutes();
	if (hou < 10) {
		hou = '0' + hou;
	}
	if (min < 10) {
		min = '0' + min;
	}
	return hou + ":" + min;
}

function formatNumber(n) {
	n = n.toString();
	return n[1] ? n : '0' + n;
}

function getDateDiff(startTime, endTime, diffType) {
	//将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式 
	startTime = startTime.replace(/\-/g, '/');
	endTime = endTime.replace(/\-/g, '/');
	//将计算间隔类性字符转换为小写
	diffType = diffType.toLowerCase();
	let sTime = new Date(startTime); //开始时间
	let eTime = new Date(endTime); //结束时间
	//作为除数的数字
	let timeType = 1;
	switch (diffType) {
		case 'second':
			timeType = 1000;
			break;
		case 'minute':
			timeType = 1000 * 60;
			break;
		case 'hour':
			timeType = 1000 * 3600;
			break;
		case 'day':
			timeType = 1000 * 3600 * 24;
			break;
		default:
			break;
	}
	return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType));
}


// 时间转换（将秒转换为时分秒格式）
function changeTimeFormat(data) {
	let duration;
	let h = parseInt(data / 3600);
	let m = parseInt((data % 3600) / 60);
	let s = (data % 3600) % 60;

	h = h < 10 ? '0' + h : h;
	m = m < 10 ? '0' + m : m;
	s = s < 10 ? '0' + s : s;
	if (parseInt(h) > 0) {
		duration = h + ':' + m + ':' + s;
	} else {
		duration = m + ':' + s;
	}
	return duration;
}

// 格式化手机号
function formatPhoneOnkeyUp(mobile) {
	var value = mobile.replace(/\D/g, '').substring(0, 11);
	var valueLen = value.length;
	if (valueLen > 3 && valueLen < 8) {
		value = `${value.substr(0, 3)} ${value.substr(3)}`;
	} else if (valueLen >= 8) {
		value = `${value.substr(0, 3)} ${value.substr(3, 4)} ${value.substr(7)}`;
	}
	return value;
}

function getTime(timestamp, all) {
	let time = arguments[0] || 0;
	let t, y, m, d, h, i, s;
	t = time ? new Date(time * 1000) : new Date();
	y = t.getFullYear(); // 年
	m = t.getMonth() + 1; // 月
	d = t.getDate(); // 日

	h = t.getHours(); // 时
	i = t.getMinutes(); // 分
	s = t.getSeconds(); // 秒

	// console.log(all)
	if (all.valueOf() === false) {
		return [y, m, d].map(formatNumber).join('.');
	} else {
		return [y, m, d].map(formatNumber).join('.') + ' ' + [h, i, s].map(formatNumber).join(':');
	}
}

//v2=>是否更换域名
function httpRequest(url, data, method, v2) {
	let m = method || 'GET';
	let d = data || {};
	// let accessToken = uni.getStorageSync('token');
	// if (accessToken) {
	// 	let expireAt = uni.getStorageSync('expire_at');
	// 	let now = Date.parse(new Date());
	// 	if (expireAt) {
	// 		if (now > expireAt) {
	// 			//移出token
	// 			uni.removeStorageSync('token');
	// 			uni.removeStorageSync('expire_at');
	// 		}
	// 	}
	// }
	return uni.pro.request({
		url,
		data: d,
		method: m,
		v2
	});
}

module.exports = {
	getDateDiff,
	httpRequest,
	//时间转换
	changeTimeFormat,
	getFormatDate,
	getFormatDate1,
	getFormatDate2,
	//手机号转换
	formatPhoneOnkeyUp,
	getTime,
	getNowFormatDate
};